// src/models/planets.model.js
const planets = [];
module.exports = planets;

const fs = require('fs');
const path = require('path');
const parse = require('csv-parse').parse;
const habitablePlanets = [];
// 通过检查⾏星数据的属性，判断⾏星是否适合居住
function isHabitablePlanet(planet) {
 return planet['koi_disposition'] === 'CONFIRMED'
 && planet['koi_insol'] > 0.36 && planet['koi_insol'] < 1.11
 && planet['koi_prad'] < 1.6;
}
// 从 CSV ⽂件中加载⾏星数据
function loadPlanetsData() {
 return new Promise((resolve, reject) => {
 fs.createReadStream(path.join(__dirname, '..','..', 'data', 'kepler_data.csv'))
 .pipe(parse({
 comment: '#',
 columns: true,
 }))
 .on('data', (data) => {
 if (isHabitablePlanet(data)) {
 habitablePlanets.push(data);
 }
 })
 .on('error', (err) => {
 console.log(err);
 reject(err);

 })
 .on('end', () => {
 console.log(`找到${habitablePlanets.length}个可居住星球！`);
 resolve();
 });
 });
}
function getAllPlanets() {
    return habitablePlanets;
   }
   module.exports = {
    loadPlanetsData,
    getAllPlanets,
   };
module.exports = {
 loadPlanetsData,
 planets: habitablePlanets,
};
